//
//  BaseNC.m
//  BaseCode
//
//  Created by 曹连华 on 2020/3/19.
//

#import "BaseNC.h"
#import "BaseVC.h"

@interface BaseNC ()

@end

@implementation BaseNC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationUI];
    
}

- (void)navigationUI {
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_image_bg"] forBarMetrics:UIBarMetricsDefault];
    [self setNaviButtonWithImageName:@"navigation_btn_back" position:BarButtonItemPositionLeft];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    BOOL isTopVC = self.viewControllers.count <= 1;
    UIViewController *currentTopVC = self.topViewController;
    currentTopVC.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];
    
    //顶层vc，需要把hidesBottomBarWhenPushed重置
    if(isTopVC) {
        if ([currentTopVC isKindOfClass:[BaseVC class]]) {//如果是VC,读取topVcTabbarHideStatus属性。e.g. BaseWebVC配置在第一屏的时候，是希望隐藏tabbar的，topVcTabbarHideStatus设置为YES,就能实现需求。
            BaseVC *temp = (BaseVC *)currentTopVC;
            currentTopVC.hidesBottomBarWhenPushed = temp.topVcTabbarHideStatus;
        } else {//如果不是VC,第一屏应该要显示tabbar，hidesBottomBarWhenPushed应该设置为no
            currentTopVC.hidesBottomBarWhenPushed = NO;
        }
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    /**
     *  nc--->tbc--->nc
     *  nc--->nc----nc
     *  这两种结构的处理
     */
    if (self.viewControllers.count == 1) {
        if (self.tabBarController && self.tabBarController.navigationController) {
            return [self.tabBarController.navigationController popViewControllerAnimated:YES];
        }
        if (self.navigationController && self.navigationController.navigationController) {
            return [self.navigationController.navigationController popViewControllerAnimated:YES];
        }
    }
    //正常结构的处理
    return [super popViewControllerAnimated:animated];
}

- (void)leftBtnAction:(UIBarButtonItem *)btn {
    [self popViewControllerAnimated:YES];
}

@end
